﻿using Anke.SHManage.Model.FixedAssetsModel;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL.FixedAssetsDAL
{
    /// <summary>
    /// 固定资产仓库维护 DAL层
    /// plq
    /// </summary>
    public class FixedAssetsStorageDAL
    {
        /// <summary>
        /// 获取所有固定资产仓库---不筛选是否有效
        /// </summary>
        /// <returns></returns>
        public List<FixedAssetsStorageInfo> GetAllStorageList()
        {
            List<FixedAssetsStorageInfo> list = new List<FixedAssetsStorageInfo>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select 仓库编码,父级仓库编码,仓库名称,是否有效,备注 from FIXED_Storage where 1= 1 ");
//            sb.Append(@"
//                       and 是否有效=1");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        FixedAssetsStorageInfo info = new FixedAssetsStorageInfo();
                        info.CangKuBianMa = int.Parse(dr["仓库编码"].ToString());
                        info.FuJiCangKuBianMa = int.Parse(dr["父级仓库编码"].ToString());
                        info.CangKuMingCheng = dr["仓库名称"].ToString();
                        info.ShiFouYouXiao = Convert.ToBoolean(dr["是否有效"]);
                        info.BeiZhu = dr["备注"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/GetStorageList", ex.Message);
                return null;
            }
          
        }

        /// <summary>
        /// 获取有效的固定资产仓库
        /// </summary>
        /// <returns></returns>
        public List<FixedAssetsStorageInfo> GetActiveAssetsStorageList()
        {
            List<FixedAssetsStorageInfo> list = new List<FixedAssetsStorageInfo>();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select 仓库编码,父级仓库编码,仓库名称,是否有效,备注 from FIXED_Storage where 1= 1 ");
            sb.Append(@"
                         and 是否有效=1");
            try
            {
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        FixedAssetsStorageInfo info = new FixedAssetsStorageInfo();
                        info.CangKuBianMa = int.Parse(dr["仓库编码"].ToString());
                        info.FuJiCangKuBianMa = int.Parse(dr["父级仓库编码"].ToString());
                        info.CangKuMingCheng = dr["仓库名称"].ToString();
                        info.ShiFouYouXiao = Convert.ToBoolean(dr["是否有效"]);
                        info.BeiZhu = dr["备注"].ToString();
                        list.Add(info);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/GetActiveAssetsStorageList", ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 添加固定资产仓库
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool AddAssetsStorage(string PID, string sName)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            int num = GetMaxID("FIXED_Storage") + 1; //获取表当前编码最大值，并+1
            sb.Append(" insert into FIXED_Storage values (");
            sb.Append(" " + num + ", "); //仓库编码
            sb.Append(" " + PID + ", "); //父级仓库编码
            sb.Append(" '" + sName + "', "); //仓库名称
            sb.Append(" 1, ");   //是否有效
            sb.Append(" '' "); //备注---默认空字符串
            sb.Append(" )");

            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/AddAssetsStorage", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 获取表某字段最大值
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <returns></returns>
        private int GetMaxID(string tablename)
        {
            int result = 0;
            StringBuilder sb = new StringBuilder();
            sb.Append(@" select 仓库编码 from " + tablename + " where 仓库编码 in (select max(cast(仓库编码 as int)) from " + tablename + " ) ");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null))
            {
                if (dr.Read())
                {
                    result = Convert.ToInt32(dr["仓库编码"]);
                }
                return result;
            }
        }

        /// <summary>
        /// 修改资产仓库名称
        /// </summary>
        /// <param name="SID"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool EditAssetsStorage(string SID, string sName)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update FIXED_Storage set ");
            sb.Append(@" 
                       仓库名称='" + sName + "'");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("仓库编码", SID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/EditAssetsStorage", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 禁用仓库
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public bool DisableAssetsStorage(string SID)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update FIXED_Storage set 是否有效=0 ");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("仓库编码", SID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/DisableAssetsStorage", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 启用仓库
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public bool EnableAssetsStorage(string SID)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update FIXED_Storage set 是否有效=1 ");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("仓库编码", SID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsStorageDAL/EnableAssetsStorage", ex.Message);
                result = false;
            }
            return result;
        }


    }
}
